package com.example.network_sdk;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class  RetrofitManager {

    private Retrofit retrofit;

    private RetrofitManager() {}
    private  volatile static  RetrofitManager retrofitManager;

    public static RetrofitManager getInstance() {
        if (retrofitManager==null){
            synchronized (RetrofitManager.class){
                if (retrofitManager==null){
                    retrofitManager=new RetrofitManager();
                }
            }
        }
        return retrofitManager;}
    public void create(){
        retrofit = new Retrofit.Builder().addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(new OkHttpClient.Builder()
                        .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                        .readTimeout(50, TimeUnit.SECONDS)
                        .connectTimeout(50,TimeUnit.SECONDS)
                        .writeTimeout(50,TimeUnit.SECONDS)
                        .build())
                .baseUrl(ApiConst.BASEURL)
                .build();
    }

    public Retrofit getRetrofit(){
        if (retrofit==null){
            create();
        }
        return retrofit;
    }
}
